/**
 * @author temsa
 * Handle the Slide switches
 * @inherit Archetype.component.abstractComponent
 * @class Slidy.components.slideManager
 * @name Slidy.components.slideManager
 */
$C.Extend(["Archetype.component.graphicalComponent"],
/** @scope Slidy.components.slideManager.prototype */
{
    name: "Slidy.components.slideManager",
    /**
     * Set up the Component dependencies
     * The component won't launch main() unless
     *  every dependencies in the setup are available
     */
    setup: {
        dependencies: {
            components: {},
            lib: [/*"scriptaculous"*/],
            html: Slidy.slides
        }
    },
    //--------------------------------------------
    //some hardcoded configuration
    //totalSlide: 12,
    //originSlide: 1,
    //currentSlide: 1,
    //containerCssClass: "slideContainer",
    //slideRoot: "Slidy.templates.slide",
    //--------------------------------------------
    /**
     * Start the Component itself
     */
    initialize: function(){

        Logger.log("total slides of the :",$H(Slidy.slides).keys().length);
        this.totalSlide= $H(Slidy.slides).keys().length;
        this.originSlide= 1;
        this.currentSlide= this.originSlide;
        Logger.debug("this.currentSlide",this.currentSlide);
        this.containerCssClass= Slidy.containerCssClass;

        // get all the slide containers available.
        this.containers = $$("." + this.containerCssClass);
        this.drawSlide();
    },
    /**
     * Increment current Slide or reset it to original slide
     * @private
     */
    _nextSlide: function() {
        if (this.currentSlide < this.totalSlide)
            this.currentSlide++;
        else
            this.currentSlide = this.originSlide;

        this.drawSlide();
    },
    /**
     * Decrement current Slide or reset it to final
     */
    _previousSlide: function() {
        if (this.currentSlide > this.originSlide)
            this.currentSlide--;
        else
            this.currentSlide = this.totalSlide;

        this.drawSlide();
    },
    
    /**
     * Draws slide when available
     * @private
     */
    _drawSlide: function(){
        Logger.debug("this.containers:", this.containers);
        this.containers.each(this.renderSlide);
        
        this.fireSlideChanged(this.currentSlide);
    },
    /**
     * Fired when a GetTotalSlides Event occurs
     * @event SetTotalSlides
     */
    onGetTotalSlides: function(){
        this.fireSetTotalSlides(this.totalSlide);
    },
    fireSetTotalSlides: _,
    /**
     * Swap to next Slide
     */
    onNextSlide: function(evtName, data){
        this.nextSlide();
        Logger.log("event " + evtName + " called. Data: " , data);
    },
    /**
     * Swap to previous Slide
     */
    onPreviousSlide: function(evtName, data){
        this.previousSlide();
        Logger.log("event " + evtName + " called. Data: " , data);
    },
    /**
     * Draws current slide when fired
     */
    onDrawCurrentSlide: function(evtName, data){
        this.drawSlide();
    },
    /**
     * Update available slide containers
     */
    onNewContainer: function(evtName, data){
        this.containers = $$("." + this.containerCssClass);
    },
    /**
     * fired when a slide is changed
     */
    fireSlideChanged: _,
    /**
     * fired when a throbber can be activated
     */
    fireThrobberStart: _,
    /**
     * fired when a throbber can be stopped
     */
    fireThrobberStop: _,

    /**
     * Update the container pointed by "elt" with
     * @param {DOM Element} elt
     */
    renderViewAsString: function(){
        Logger.debug("this.currentSlide",this.currentSlide);
        return this.templates[this.currentSlide].evaluate();
    },
    _renderAndAppear: function(elt) {
        this.render(elt);
        /*new Effect.Opacity(elt,
        {
            duration: 0.5,
            fps : 50,
            transition: Effect.Transitions.linear,
            from: 0.0,
            to: 1.0
        });*/
    },
    /**
     * 
     */
    renderSlide: function(elt) {
        /*new Effect.Opacity(elt,
        {
            duration: 0.5,
            fps : 50,
            transition: Effect.Transitions.linear,
            from: 1.0,
            to: 0.0,
            afterFinish: this.renderAndAppear
        });*/
        this.renderAndAppear(elt);
    }
});
